<?php $title = "Edit Profile"; ?>
<?php include_once("header.php"); ?>
<?php
    //If you want to navigate to a page then change this variable
    $settingPage = 0;
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (isset($_POST["formSource"])) {
            switch ($_POST["formSource"]) {
                case "cPassword":
                    if (isset($_POST["password"]) && isset($_POST["oldPassword"])) {
                        $change_password = $_POST["password"];
                        $old_password = $_POST["oldPassword"];
                        if ($page->changePassword($old_password, $change_password)) {
                            $page->addMessage("Password Changed");
                        } else {
                            $page->addMessage("Password Change failed");
                        }
                        $settingPage = 1;
                    }
                    break;
                case "cAccount" :
                    if (isset($_POST["firstName"])) {
                        $firstName = core::sanitize($_POST["firstName"]);
                        $lastName = core::sanitize($_POST["lastName"]);
                        $accountType = (isset($_POST["accountType"]) ? $_POST["accountType"] : $page->getAccountType());
                        $email = $_POST["email"];
                        $skills = core::sanitize($_POST["skills"]);
                        $gender = $_POST["gender"];
                        $address = core::sanitize($_POST["address"]);
                        $contactNumber = core::sanitize($_POST["contactNumber"]);

                        if ($page->match($email, P_E)) {
                            //email has not been changed
                            $page->updateProfile($firstName, $lastName, $accountType, $email, $skills, $gender, $contactNumber, $address, false);
                            $page->addMessage("Profile Updated!");
                        } else {
                            //email has been changed
                            $page->updateProfile($firstName, $lastName, $accountType, $email, $skills, $gender, $contactNumber, $address, true);
                            $page->addMessage("Profile Updated!");
                        }
                        $settingPage = 0;
                    }
                    break;
                case "cUniversity" :
                    if (isset($_POST["universityId"])) {
                        $universityId = core::sanitize($_POST["universityId"]);
                        $degreeName = core::sanitize($_POST["degreeName"]);
                        $majors = core::sanitize($_POST["major"]);
                        $yod = core::sanitize($_POST["year"]);
                        $entryToUts = core::sanitize($_POST["entry"]);
                        $highSchool = core::sanitize($_POST["highSchool"]);
                        $eca = core::sanitize($_POST["extraCurricular"]);
                        $scholarShips = core::sanitize($_POST["scholarships"]);
                        $previousInternships = core::sanitize($_POST["intership"]);
                        $internshipOrganized = $_POST["internOrganised"];

                        $page->updateUniversityDetails($universityId, $degreeName, $majors, $yod, $entryToUts, $highSchool, $eca, $scholarShips, $previousInternships, $internshipOrganized);
                        $page->addMessage("University Details Updated!");
                        $settingPage = 2;
                    }
                    break;
                case "cOther":
                    $availaibity = Core::sanitize($_POST["availability"]);
                    $workshops = Core::sanitize($_POST["workshops"]);
                    $driversLicence = $_POST["licence"];
                    $refugee = $_POST["refugee"];
                    $indigenous = $_POST["indigenous"];
                    $page->updateEditProfileOtherDetails($availaibity, $workshops, $driversLicence, $refugee, $indigenous);
                    $page->addMessage("Others details Updated!");

                    $settingPage = 3;
                    break;
                case "cApplied":
                        $roleId = $_POST["appliedRole"];
                        $attended = ($_POST["attended"] == "true" ? 1 : 0);
                        $hoursWorked = Core::sanitize($_POST["hours"]);
                        $page->updateRoleApplied($personId,$roleId,$attended,$hoursWorked);
                        $page->addMessage("Applied Role Details updated");
                        $settingPage = 4;
                    break;
            }
        }
    }
?>
<div class="centerContent">
    <label class="pageMessage"><?php $page->printMessages(); ?></label>
    <div id="accountButtons" class="accountButtons">
        <input class="button" type="button" value="Account Information"/>
        <input class="button" type="button" value="Password"/>
        <input class="button" type="button" value="University"/>
        <input class="button" type="button" value="Others"/>
        <input class="button" type="button" value="Applied"/>
    </div>
    <br/>
    <?php include_once("WEB-INF/account.php"); ?>
    <?php include_once("WEB-INF/password.php"); ?>
    <?php include_once("WEB-INF/university.php"); ?>
    <?php include_once("WEB-INF/other.php"); ?>    
    <?php include_once("WEB-INF/applied.php"); ?>

</div>
<script type="text/javascript">
    //Edit profile module
    var editProfile = (function() {
        var forms;
        var buttons;
        var page = parseInt(<?php echo $settingPage ?>); //tab to load onload

        window.onload = function() {
            buttons = UTSDK.$("accountButtons");
            forms = document.getElementsByTagName("form");
            forms = [].slice.call(forms);
            forms.splice(0, 1); //remove login form
            //Loop through tab forms and setup contents
            for (var i = 0; i < buttons.children.length; i++) {
                buttons.children[i].addEventListener("click", changeSelected);
                forms[i].style.display = "none";
                var json = {};
                json[forms[i].elements[2].name] = <?php echo $_SESSION["personId"] ?>;
                json["id"] = forms[i].id;
                //Update the hidden select to current user
                UTSDK.updateSelect(json, forms[i].elements[2], "update.php", function(data) {
                    var form = UTSDK.$(data["id"]);
                    var select = form.elements[2];
                    select.value = select.options[1].value;
                    if (form.id === "appliedForm") {
                        appliedForm.updateRoles();
                    }
                    else {
                        var json = {};
                        json["formSource"] = form.elements[0].value;
                        json[form.elements[2].name] = form.elements[2].value;
                        json["id"] = form.id;
                        UTSDK.updateForm(json, "update.php");
                    }
                });
                //
                var id = forms[i].id.substring(0, forms[i].id.length - 4);
                UTSDK.$(id + "Submit").value = "Save";
                forms[i].elements[1].value = "update";
            }
            if (page !== null)
                navigate(page);
            else
                navigate(0);

            enableHide();
        };

        //Navigate to tab
        function navigate(page) {
            UTSDK.addClass(buttons.children[page], "selectedButton");
            forms[page].style.display = "block";
        }

        //Change selected tab
        function changeSelected(evt) {
            var parent = evt.target.parentElement;
            for (var i = 0; i < parent.children.length; i++) {
                if (parent.children[i] === evt.target) {
                    UTSDK.addClass(parent.children[i], "selectedButton");
                    UTSDK.fadeIn(forms[i], 0.7);
                }
                else {
                    UTSDK.removeClass(parent.children[i], "selectedButton");
                    UTSDK.fadeOut(forms[i], 0.7);
                }
            }
        }

        //Hide elements not used in editprofile
        function enableHide() {
            UTSDK.$("userAccount").setAttribute("data-required", "false");
            UTSDK.$("activated").setAttribute("data-required", "false");
            UTSDK.$("passwordAccount").setAttribute("data-required", "false");
            UTSDK.$("newPassword").setAttribute("data-required", "false");
            UTSDK.$("newRetypePassword").setAttribute("data-required", "false");
            UTSDK.$("universityAccount").setAttribute("data-required", "false");
            UTSDK.$("othersAccount").setAttribute("data-required", "false");
            UTSDK.$("appliedAccount").setAttribute("data-required", "false");
            UTSDK.$("userAccount").parentElement.parentElement.style.display = "none";
            UTSDK.$("passwordAccount").parentElement.parentElement.style.display = "none";
            UTSDK.$("newPassword").parentElement.parentElement.style.display = "none";
            UTSDK.$("activated").parentElement.parentElement.parentElement.style.display = "none";
            UTSDK.$("newRetypePassword").parentElement.parentElement.style.display = "none";
            UTSDK.$("universityAccount").parentElement.parentElement.style.display = "none";
            UTSDK.$("othersAccount").parentElement.parentElement.style.display = "none";
            UTSDK.$("appliedAccount").parentElement.parentElement.style.display = "none";
            UTSDK.$("paperwork").parentElement.parentElement.style.display = "none";
            UTSDK.$("contract").parentElement.parentElement.style.display = "none";
            UTSDK.$("offer").parentElement.parentElement.style.display = "none";
        }
    })();
</script>
<?php include_once("footer.php"); ?>